package cn.liccshop.core.service.shopTransaction.controller;


import cn.liccshop.core.entity.ShopTransaction;
import cn.liccshop.core.service.shopTransaction.service.IShopTransactionService;
import cn.liccshop.core.utils.ObjectToOtherUtils;
import cn.liccshop.sys.base.MyActionController;
import cn.liccshop.sys.constants.Constants;
import cn.liccshop.sys.entity.Result;
import cn.liccshop.sys.exception.MySystemException;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * @description 交易信息表 前端控制器
 *
 * @author LiCC
 * @since 2019-05-26
 */
@RestController
@RequestMapping("/shopTransaction")
public class ShopTransactionController extends MyActionController<ShopTransaction> {

    @Autowired
    private IShopTransactionService liShopTransactionService;

    @Override
    protected IService<ShopTransaction> getLiService(){
        return liShopTransactionService;
    }

    /**
     * 通过类行，交易编号，订单编号，手机号，金额范围，支付时间，申请退款时间,退款时间范围获取支付信息。
     * @param page
     * @param size
     * @param map
     * @return
     * @throws MySystemException
     */
    @RequestMapping(value = "infosByPage")
    public Result getInfosByPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestBody Map<String, Object> map) throws MySystemException {
        Integer type = ObjectToOtherUtils.toInteger(map.get("type"));
        String transactionSn = ObjectToOtherUtils.toString(map.get("transactionSn"));
        String orderSn = ObjectToOtherUtils.toString(map.get("orderSn"));
        String phone = ObjectToOtherUtils.toString(map.get("phone"));
        Integer minAmount = ObjectToOtherUtils.toInteger(map.get("minAmount"));
        Integer maxAmount = ObjectToOtherUtils.toInteger(map.get("maxAmount"));
        String beginPayTime = ObjectToOtherUtils.toString(map.get("beginPayTime"));
        String endPayTime = ObjectToOtherUtils.toString(map.get("endPayTime"));
        String beginApplayReturnTime = ObjectToOtherUtils.toString(map.get("beginApplayReturnTime"));
        String endApplayReturnTime = ObjectToOtherUtils.toString(map.get("endApplayReturnTime"));
        String beginReturnTime = ObjectToOtherUtils.toString(map.get("beginReturnTime"));
        String endReturnTime = ObjectToOtherUtils.toString(map.get("endReturnTime"));
        Wrapper<ShopTransaction> wrapper = new QueryWrapper<>(new ShopTransaction());
        if (type!=null&&(type==1||type==2||type==3)){
            ((QueryWrapper<ShopTransaction>) wrapper).eq("type", type);
        }
        if (type==null||type==1){
            /** 订单没有条件筛选 或 订单完成状态 按支付时间降序**/
            ((QueryWrapper<ShopTransaction>) wrapper).orderByDesc("pay_time");
        }
        if (type!=null&&type==2){
            /** 订单申退状态 按申请退款时间降序**/
            ((QueryWrapper<ShopTransaction>) wrapper).orderByDesc("applay_return_time");
        }
        if (type!=null&&type==3){
            /** 订单退款状态 按退款时间降序 **/
            ((QueryWrapper<ShopTransaction>) wrapper).orderByDesc("return_time");
        }
        if (StringUtils.isNotBlank(transactionSn)){
            ((QueryWrapper<ShopTransaction>) wrapper).like("transaction_sn", transactionSn);
        }
        if (StringUtils.isNotBlank(orderSn)){
            ((QueryWrapper<ShopTransaction>) wrapper).like("order_sn", orderSn);
        }
        if (StringUtils.isNotBlank(phone)){
            ((QueryWrapper<ShopTransaction>) wrapper).like("phone", phone);
        }
        if (minAmount!=null&&minAmount>=0){
            if (maxAmount!=null&&maxAmount.intValue()>minAmount.intValue()){
                ((QueryWrapper<ShopTransaction>) wrapper).le("amount",maxAmount).ge("amount",minAmount);
            } else {
                ((QueryWrapper<ShopTransaction>) wrapper).ge("amount",minAmount);
            }
        } else if (maxAmount!=null&&maxAmount>0){
            ((QueryWrapper<ShopTransaction>) wrapper).le("amount",maxAmount);
        }
        if (StringUtils.isNotBlank(beginPayTime)){
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            ((QueryWrapper<ShopTransaction>) wrapper).between("pay_time", LocalDateTime.parse(beginPayTime,df),LocalDateTime.parse(endPayTime,df));
        }
        if (StringUtils.isNotBlank(beginApplayReturnTime)){
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            ((QueryWrapper<ShopTransaction>) wrapper).between("applay_return_time", LocalDateTime.parse(beginApplayReturnTime,df),LocalDateTime.parse(endApplayReturnTime,df));
        }
        if (StringUtils.isNotBlank(beginReturnTime)){
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            ((QueryWrapper<ShopTransaction>) wrapper).between("return_time", LocalDateTime.parse(beginReturnTime,df),LocalDateTime.parse(endReturnTime,df));
        }
        ((QueryWrapper<ShopTransaction>) wrapper).orderByDesc("create_date");
        IPage<ShopTransaction> iPage = new Page<>(page, size);
        liShopTransactionService.page(iPage, wrapper);
        return new Result().setCode(Constants.OK).setData(iPage);
    }


    /**
     * 通过性别统计销费额
     * @return
     */
    @RequestMapping(value = "infosGroupBySex")
    public Result transactionVChartsInfoBySex(){
        Map<String, Object> data = liShopTransactionService.transactionVChartsInfoBySex();
        return new Result().setCode(Constants.OK).setData(data).setMessage("获取信息成功");
    }


    /**
     * 支付宝退款接口
     * @return
     */
    @RequestMapping(value = "returnById")
    public Result transactionReturnById(@RequestBody Map<String, Object> map) throws MySystemException {
        String id = (String) map.get("id");
        if (StringUtils.isBlank(id)){
            throw new MySystemException("参数异常", Constants.INVALID_PARM);
        }
        Map<String, Object> isSuccess = liShopTransactionService.transactionReturnById(id);
        return new Result().setCode((Integer) isSuccess.get("code")).setMessage(isSuccess.get("message"));
    }
}

